package com.yzjs.localhost.service.hrmRecruitment.impl;

import com.alibaba.fastjson.JSONObject;
import com.yzjs.common.base.service.BaseServiceImpl;
import com.yzjs.common.util.conversion.JsonUtil;
import com.yzjs.localhost.dao.hrmRecruitment.HrmRecruitmentMapper;
import com.yzjs.localhost.dto.ComparisonResultDTO;
import com.yzjs.localhost.dto.DataBundlingDTO;
import com.yzjs.localhost.dto.UserRightsDTO;
import com.yzjs.localhost.entity.hrmRecruitment.HrmRecruitmentEntity;
import com.yzjs.localhost.entity.tDataPersistenceAfter.TDataPersistenceAfterEntity;
import com.yzjs.localhost.entity.tDataPersistenceBeforce.TDataPersistenceBeforceEntity;
import com.yzjs.localhost.service.AnalysisService;
import com.yzjs.localhost.service.hrmRecruitment.HrmRecruitmentService;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("hrmRecruitmentService")
public class HrmRecruitmentServiceImpl extends BaseServiceImpl<HrmRecruitmentEntity, HrmRecruitmentMapper > implements HrmRecruitmentService {


    @Override
    public ComparisonResultDTO HrmRecruitmentCompliance(DataBundlingDTO dto, UserRightsDTO rights) throws Exception {
        TDataPersistenceBeforceEntity beforce =dto.getBeforce();
        String beforceInfo  = beforce.getInfo();
        Map<String,Object> beforceParse = JsonUtil.json2map(beforceInfo);
        TDataPersistenceAfterEntity after = dto.getAfter();
        String afterInfo  = after.getInfo();

        ComparisonResultDTO resultDTO = null ;
        if (beforce.getOperatorCode().contains("add")){
            Map<String,Object>  afterParse = JsonUtil.json2map(afterInfo);
            // 比对 部门ID,人员ID，姓名，证件号码，电话，户籍地址
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(),beforceParse,afterParse,"staffDeptId,staffUserId,fullName,credentialsNum,telPhone,permanentAddress");
        }else if (beforce.getOperatorCode().contains("edit")){
            Map<String,Object>  afterParse = JsonUtil.json2map(afterInfo);
            // 比对 部门ID,人员ID，姓名，证件号码，电话，户籍地址
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(),beforceParse,afterParse,"staffDeptId,staffUserId,fullName,credentialsNum,telPhone,permanentAddress");
        } else if (beforce.getOperatorCode().contains("remove")){
            beforceParse.put("deleteFlag","2");
            Map<String,Object>  afterParse = JsonUtil.json2map(afterInfo);
            // 比对 人员ID
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(),beforceParse,afterParse,"staffUserIds,deleteFlag");
            Object kycgId = afterParse.get("staffUserIds");
            if (kycgId == null) {
                resultDTO.setAbstractName("主键ID不存在");
                resultDTO.setWarningInfo("主键ID不存在");
                resultDTO.setMathFlag(false);
                return  resultDTO;
            }
            HrmRecruitmentEntity hrmRecruitmentEntity = new HrmRecruitmentEntity();
            hrmRecruitmentEntity.setStaffUserId(Long.valueOf((String) afterParse.get("staffUserIds")));
            hrmRecruitmentEntity.setDeleteFlag((String) afterParse.get("deleteFlag"));
            hrmRecruitmentEntity.setDataIdentification(after.getDataIdentification());
            this.insert(hrmRecruitmentEntity);
            return resultDTO;
        }
        // 记录表表信息
        HrmRecruitmentEntity hrmRecruitmentEntity = JsonUtil.json2pojo(after.getInfo(), HrmRecruitmentEntity.class);
        hrmRecruitmentEntity.setPermissionMirroring(JsonUtil.obj2jsonIgnoreNull(rights));
        hrmRecruitmentEntity.setDataIdentification(after.getDataIdentification());
        this.insert(hrmRecruitmentEntity);

        return resultDTO;
    }
}